package slowka;

import java.beans.*;
import java.util.Random;

/** Contains translation of a word in two languages */
public class Translation extends XMLSerializable {
    public Translation(Word W1, Word W2) {
        from = W1;
        to = W2;
    }

    public Word getFrom() {
        return from;
    }

    /** Gets word translated in given language
     * @param lang target language of the translation
     */
    public Word getWord(String lang) {
        if(from.getLanguage().contentEquals(lang))
            return from;
        else
            return to;
    }

    /** Orders the translation, to be from given language
     * @param fromLang language, from which the translation should be, or keyword "Random" to randomize
     */
    public void setFromToOrder(String fromLang) {
        if(fromLang.contentEquals("RANDOM")) {
            Random r = new Random();
            if(r.nextBoolean())
                setFromToOrder(to.getLanguage());
        }
        else if(to.getLanguage().contentEquals(fromLang)) {
            Word tmp = from;
            from = to;
            to = tmp;
        }
    }

    public Word getTo() {
        return to;
    }

    private Word from, to;

    public static void extractPersistanceDelegates(XMLEncoder e) {
        Word.extractPersistanceDelegates(e);
        e.setPersistenceDelegate(Translation.class, new DefaultPersistenceDelegate(new String[] {"from", "to"}));
    }
}
